package com.atguigu.flink.chapter11.function;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/8/21 9:59
 */
public class Flink04_TableAgg {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        DataStreamSource<WaterSensor> waterSensorStream =
            env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                             new WaterSensor("sensor_1", 2000L, 20),
                             new WaterSensor("sensor_2", 3000L, 30),
                             new WaterSensor("sensor_1", 4000L, 40),
                             new WaterSensor("sensor_1", 5000L, 50),
                             new WaterSensor("sensor_2", 6000L, 60));
        
        Table table = tenv.fromDataStream(waterSensorStream);
        
        // 1. table api使用
        // 1.1 内联
        
        table
            .groupBy($("id"))
            .flatAggregate(call(MyTableAgg.class, $("vc")).as("value"))
            .select($("id"), $("value"))
            .execute()
            .print();
        
        // 1.2 注册后使用
        //略
        // 2. sql中使用
        
    }
    
    public static class Top2 {
        public Integer first = 0;
        public Integer second = 0;
    }
    
    public static class MyTableAgg extends TableAggregateFunction<String, Top2> {
        
        // 初始化累加器
        @Override
        public Top2 createAccumulator() {
            return new Top2();
        }
        
        // 对值累加  (聚合)
        public void accumulate(Top2 acc, Integer vc) {
            if (vc > acc.first) {
                acc.second = acc.first;
                acc.first = vc;
            }else if(vc > acc.second){
                acc.second = vc;
            }
        }
        
        // 把值放入到表中  (指标)
        public void emitValue(Top2 acc, Collector<String> out) {
            out.collect("最高: " + acc.first);
            if (acc.second != 0) {
                out.collect("第二: " + acc.second);
            }
        }
        
    }
    
}
/*
每来一行数据:
    输出两行top2的水位(两行)
new WaterSensor("sensor_1", 1000L, 10)
    sensor_1     10
    sensor_1     0
    
 new WaterSensor("sensor_1", 2000L, 20)
     sensor_1    20
    sensor_1     10
    
  
 */
